package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.ShiftToAdapter
import com.ht.medicaltreatment.bean.OrderListBean
import com.ht.medicaltreatment.viewmodel.OrderListModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.shift_to_layout.imgArrow
import kotlinx.android.synthetic.main.shift_to_layout.llAll
import kotlinx.android.synthetic.main.shift_to_layout.recyclerView
import kotlinx.android.synthetic.main.shift_to_layout.tvAll

//转入
class ShiftToActivity : BaseActivity(), View.OnClickListener, OnItemClickListener {
    private val model by viewModels<OrderListModel>()

    override fun getLayoutId() = R.layout.shift_to_layout
    private var isOpen = false
    private lateinit var adapter:ShiftToAdapter

    override fun init() {
        setTitle("转入列表")

        llAll.setOnClickListener(this)
        recyclerView.layoutManager = GridLayoutManager(this,3)
        adapter = ShiftToAdapter()
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
    }

    override fun onResume() {
        super.onResume()
        showProgress()
        if(!isOpen){
            tvAll.text = "显示全部"
            imgArrow.setBackgroundResource(R.drawable.icon_arrow_bottom)
            model.getList(OrderListModel.IN,{
                adapter.setNewInstance(it)
                dismissProgress()
            },{
                dismissProgress()
                showToast(it)
            })
        }else{
            tvAll.text = "收起"
            imgArrow.setBackgroundResource(R.drawable.icon_arrow_top)
            model.getAllList(OrderListModel.IN,{
                adapter.setNewInstance(it)
                dismissProgress()
            },{
                dismissProgress()
                showToast(it)
            })
        }
    }

    override fun onClick(v: View?) {
        showProgress()
        if(isOpen){
           isOpen = false
            tvAll.text = "显示全部"
            imgArrow.setBackgroundResource(R.drawable.icon_arrow_bottom)
            model.getList(OrderListModel.IN,{
                adapter.setNewInstance(it)
                dismissProgress()
            },{
                dismissProgress()
                showToast(it)
            })
        }else{
            isOpen = true
            tvAll.text = "收起"
            imgArrow.setBackgroundResource(R.drawable.icon_arrow_top)
            model.getAllList(OrderListModel.IN,{
                adapter.setNewInstance(it)
                dismissProgress()
            },{
                dismissProgress()
                showToast(it)
            })
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as OrderListBean
        val intent = Intent(this,CaiYangBaoActivity::class.java)
        intent.putExtra("id",item.id)
        startActivity(intent)
    }
}
